package com.cems.controller.forumNie;

import com.cems.pojo.*;
import com.cems.service.ForumService;
import com.cems.service.UserService;
import com.cems.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/*
 *
 *@auth  biewenzhao
 *@date 2021/5/25  19:26
 */
@Controller
public class ForumNieController {

    @Autowired
    ForumService forumService;

    @Autowired
    UserService userService;

    @GetMapping("toForumList")
    public String toForumList(HttpSession session) {

        List<ForumArticle> forumArticles = forumService.getAllArticle2();
        int pages;
        if (forumArticles.size() % 5 == 0) {
            pages = forumArticles.size() / 5;
        } else {
            pages = forumArticles.size() / 5 + 1;
        }
        ArrayList<String> forumidList = new ArrayList<>();
        ArrayList<String> userIdList = new ArrayList<>();
        ArrayList<String> foTitleList = new ArrayList<>();
        ArrayList<String> foDateList = new ArrayList<>();

        for (ForumArticle ForumArticle : forumArticles) {
            forumidList.add(String.valueOf(ForumArticle.getId()));
            userIdList.add(String.valueOf(ForumArticle.getUserId()));
            foTitleList.add(String.valueOf(ForumArticle.getFoTitle()));
            foDateList.add(String.valueOf(ForumArticle.getFoDate()));
        }
        session.setAttribute("forumidList", forumidList);
        session.setAttribute("userIdList", userIdList);
        session.setAttribute("foTitleList", foTitleList);
        session.setAttribute("foDateList", foDateList);
        System.out.println("ASDASDASD_____??????>>>>>" + pages);
        session.setAttribute("LeisureEntrustspages", pages);
        return "font/community/forumShow";
    }

    @GetMapping("toAddForum")
    public String toAddForum(HttpSession session, Model model) {
        SysUser user;
        Integer userId;
        try {
            user = (SysUser) session.getAttribute("user");
            userId = user.getId();
        } catch (Exception e) {
            return "User/userlogin";
        }

        model.addAttribute("userId", userId);
        return "font/community/addForum";
    }

    /**
     * 添加帖子
     *
     * @param hidden   发帖人的id
     * @param pname    帖子标题
     * @param adminDec 帖子内容
     * @return
     */
    @GetMapping("addForum")
    @ResponseBody
    public String addForum(String hidden, String pname, String adminDec) {
        System.out.println("hidden+++>>>>" + hidden);
        System.out.println("pname+++>>>>" + pname);
        System.out.println("adminDec+++>>>>" + adminDec);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", Integer.valueOf(hidden));
        map.put("foTitle", pname);
        map.put("foDate", adminDec);
        forumService.addForum(map);

        return pname + ":" + "发布完毕";
    }
    //自己的帖子
    @GetMapping("Articlemessages/{id}")
    public String Articlemessages(@PathVariable String id, Model model){

        List<ForumArticle> forumArticles = forumService.selArticleByUId(Integer.valueOf(id));
        model.addAttribute("Articlemessages",forumArticles);
        return "message/Article";
    }
    //回收站的帖子
    @GetMapping("ArticleDelmessages/{id}")
    public String ArticleDelmessages(@PathVariable String id, Model model){
        List<ForumArticle> forumArticles = forumService.selDelArticleByUId(Integer.valueOf(id));
        model.addAttribute("ArticleDelmessages",forumArticles);
        return "message/DelArticle";
    }
    @GetMapping("delArticle")
    @ResponseBody
    public String delArticle(Integer id){
        ForumArticle forumArticle = forumService.selArticleById(id);
        forumService.delArticeById(id);
        return String.valueOf(forumArticle.getUserId());
    }

    @GetMapping("chakanArticle")
    @ResponseBody
    public String chakanArticle(Integer id){
        ForumArticle forumArticle = forumService.selArticleById(id);
        List<ForumCommentBig> forumCommentListByArt = forumService.getForumCommentListByArt(id);

        if (forumArticle.getStatus().equals("待审核")){
            return "";
        }else {
            return "ok";
        }
    }
}
